//
//  AccountsView.m
//  shoppingMall
//
//  Created by Bear on 2016/10/27.
//  Copyright © 2016年 dragontrail. All rights reserved.
//

#import "AccountsView.h"
#import "UIButton+CustomStyle.h"
#import "GoodsManager.h"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)


@implementation AccountsView
{
    float height;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    height = frame.size.height;
    self = [super initWithFrame:frame];
    if (self) {
        [self creatLayout];
        self.backgroundColor = [UIColor whiteColor];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cancelSelectedAll) name:@"CancelSomeGoodsSelected" object:nil];
        [[GoodsManager defaultManager] addObserver:self forKeyPath:@"goodsTotalPrice" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

-(void)creatLayout{

    self.accountsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.accountsBtn setBackgroundColor:[UIColor colorWithRed:252/255.00 green:86/255.00 blue:31/255.00 alpha:1.0]];
    [self.accountsBtn setTitle:@"结算(0)" forState:UIControlStateNormal];
    [self.accountsBtn setFrame:CGRectMake(SCREEN_WIDTH/4*3 , 0,SCREEN_WIDTH/4,height)];
    [self addSubview:self.accountsBtn];

    [self.accountsBtn addTarget:self action:@selector(accountsAction:) forControlEvents:UIControlEventTouchUpInside];

    self.selectAllBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.selectAllBtn goodsSelectStyle];
    [self.selectAllBtn setFrame:CGRectMake(5, (height-30)/2, 30, 30)];
    [self addSubview:self.selectAllBtn];
    [self.selectAllBtn addTarget:self action:@selector(selectAllAction:) forControlEvents:UIControlEventTouchUpInside];

    UILabel* lable =[[UILabel alloc] initWithFrame:CGRectMake(40, 0, 40, height)];
    lable.text = @"全选";
    [self addSubview:lable];

    self.totalLable = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/4*3-180, 0, 120, height)];
    self.totalLable.font = [UIFont systemFontOfSize:14];
    self.totalLable.adjustsFontSizeToFitWidth = YES;
    self.totalLable.textAlignment = NSTextAlignmentRight;
    self.totalLable.text = [NSString stringWithFormat:@"合计:￥%.2f",[GoodsManager defaultManager].goodsTotalPrice];;
    self.isFreightLable = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width/4*3-60, 0, 60, height)];
    self.isFreightLable.textAlignment = NSTextAlignmentCenter;
    self.isFreightLable.text = @"含运费";
    self.isFreightLable.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.isFreightLable];
    [self addSubview:self.totalLable];
}

-(void)cancelSelectedAll{

    [self.selectAllBtn setSelected:NO];
}

-(void)accountsAction:(UIButton*)sender{

    if (self.accoutsAllActionBlock) {
        self.accoutsAllActionBlock(sender);
    }

}

-(void)selectAllAction:(UIButton*)sender{

    if (self.selectAllActionBlock) {
        self.selectAllActionBlock(sender);
    }

}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{

    self.totalLable.text = [NSString stringWithFormat:@"合计:￥%@",change[@"new"]];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[GoodsManager defaultManager] removeObserver:self forKeyPath:@"goodsTotalPrice"];

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
